<?php

namespace Cream;

use Cream\Backend\Core\Backend as Backend;
use Cream\Core\Plugins as plugins;

require realpath(dirname(__FILE__)) . '/../../../../core/paths.php';
require __CORE__ . 'core.php';

$_APP = new Backend($system);
$_APP->setCache(false);


switch ($_GET['mode']) {
	case 'cities':
		$data = $_APP->_CRUD->readById('* from villes 
			where name like \'' . mysql_real_escape_string($_GET['q']) . '%\'');
		if($data){
			foreach($data as $k => $v){
				echo utf8_encode($v['name']) . "\n";
			}	
		}
		break;
	
	case 'gibc': // get infos by city name
		$data = $_APP->_CRUD->read('v.zipcode,d.name as dname,r.name as rname from villes as v 
			left join departements as d on d.id=v.deptid 
			left join region as r on r.id=d.regionid
			where v.name = \'' . mysql_real_escape_string(ucfirst($_POST['q'][0])) . '\'');
		if($data){
			echo $data[0]['zipcode'] . '|' . utf8_encode($data[0]['dname']) . '|' . utf8_encode($data[0]['rname']);
		}
		break;
}